<template>
  <div class="login">
    <div class="login-box">
      <h3>XXXX管理系统</h3>
      <div class="login-input-box">
        <i class='bx bx-user'></i>
        <input class="login-input" type="text" placeholder="请输入用户名" v-model="userName">
      </div>
      <div class="login-input-box">
        <i class='bx bx-lock'></i>
        <input class="login-input" type="password" placeholder="请输入密码" v-model="userPwd">
      </div>
      <button class="btn" @click="login">登&nbsp;&nbsp;录</button>
      <p class="err-msg" v-show="isErr">{{ errMsg }}</p>
    </div>
  </div>
</template>

<script>
// import userData from '@/assets/json/user.json'
import axios from 'axios'

export default {
  name: 'login',
  data () {
    return {
      userName: '',
      userPwd: '',
      errMsg: '',
      isErr: false
    }
  },
  methods: {
    login () {
      let $this = this
      if (this.userName === '') {
        alert('用户名不能为空')
      } else if (this.userPwd === '') {
        alert('密码不能空')
      } else {
        axios.post('/api/login', {
          userName: $this.userName,
          userPwd: $this.userPwd
        }).then(function (res) {
          if (res.status === 200) {
            if (res.data.success === true) {
              sessionStorage.setItem('userName', $this.userName)
              sessionStorage.setItem('userPwd', $this.userPwd)
              $this.$router.push('/home')
            } else {
              $this.isErr = true
              $this.errMsg = res.data.msg
            }
          }
        }).catch(function (err) {
          console.log(err)
        })
      }
    }
  }
}
</script>

<style scope>
.login {
  width: 100%;
  height: 100%;
  background-color: #dcdcdc;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-box {
  width: 400px;
  height: 300px;
  padding: 20px;
  border-radius: 3px;
  background-color: #fff;
  box-shadow: 2px 2px 8px #ccc;
  box-sizing: border-box;
  color: #616161;
}
.login-box h3 {
  text-align: center;
}
.login-input-box {
  width: 100%;
  padding-left: 10px;
  height: 36px;
  line-height: 36px;
  font-size: 14px;
  color:#616161;
  border: 1px solid #ccc;
  border-radius: 3px;
  margin-bottom: 10px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}
.login-input-box i {
  font-size: 16px;
  margin-right: 10px;
}
.login-input {
  flex: 1;
  border: none;
}
.err-msg {
  text-align: center;
  margin: 20px;
  color: #c72a2a;
}
</style>
